# -*- coding: utf-8 -*-
# @Time    : 2020/3/18 上午11:28
# @Author  : lilong

"""
参考链接：https://www.cnblogs.com/haiyan123/p/8260645.html
"""


# 方式一
class MyType(type):
    """继承type的就是元类"""

    def __init__(self, *args, **kwargs):
        print("MyType创建的对象", self)  # Foo
        super(MyType, self).__init__(*args, **kwargs)

    def __call__(self, *args, **kwargs):
        obj = super(MyType, self).__call__(*args, **kwargs)
        print("类创建对象", self, obj)  # Foo


class Foo(metaclass=MyType):
    # 对象加括号会去执行__call__方法，__call__方法里面继承了type的__call__方法,
    # type的__call__方法里面会先执行__new__方法，再去执行__init__方法。
    #  所以，Foo就是用type创建出来的
    user = "haiyan"
    age = 18


obj = Foo()
print("####################")


# 方式二
class MyType(type):
    def __init__(self, *args, **kwargs):
        print("ssss")
        super(MyType, self).__init__(*args, **kwargs)

    def __call__(cls, *args, **kwargs):
        v = dir(cls)
        obj = super(MyType, cls).__call__(*args, **kwargs)
        return obj


# 对象加括号就会去执行__call__方法
# MyType('Zcc', (object,), {})相当于class Zcc(object),也就是创建了一个Zcc的类
class Foo(MyType('Zcc', (object,), {})):
    user = 'haiyan'
    age = 18


obj = Foo()
print("####################")
